package com.threeonemeeting.activity;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.mobclick.android.MobclickAgent;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.FileListAdapter;

public class SelectFileActivity extends Activity {

	protected File currentFile = null;
	private static String strFileType = "*/*";

	private static final FileFilter IMAGES_FILTER = new FileFilter() {

		public boolean accept(File f) {
			return f.isDirectory()
					|| f.getName().matches("^.*?\\.(" + strFileType + ")$");
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_directory_list);

		strFileType = getIntent().getExtras().getString("type");

		// 首先读取/目录下的文件
		currentFile = new File("/sdcard");
		fill(currentFile);

		Button backBtn = (Button) findViewById(R.id.btn_back);
		Button upBtn = (Button) findViewById(R.id.btn_up);
		Button rootBtn = (Button) findViewById(R.id.btn_root);

		backBtn.setOnClickListener(backBtnClickListener);
		upBtn.setOnClickListener(upDirBtnClickListener);
		rootBtn.setOnClickListener(rootDirBtnClickListener);
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void fill(File folder) {

		List<File> files = new ArrayList<File>();

		if (strFileType.equals("*") == true
				|| strFileType.equals("*/*") == true) {
			for (File file : folder.listFiles()) {
				files.add(file);
			}
		} else {

			File[] allFiles = folder.listFiles(IMAGES_FILTER);
			if (allFiles != null) {
				for (File file : allFiles) {
					files.add(file);
				}
			} else {
				MessageBox.showToast(this, "您没有权限查看该目录");
				return;
			}

		}

		ListView fileListView = (ListView) findViewById(R.id.file_list);
		FileListAdapter fileListAdapter = new FileListAdapter(this, files,
				fileListView);
		fileListView.setAdapter(fileListAdapter);
	}

	// 根目录
	protected OnClickListener backBtnClickListener = new OnClickListener() {

		public void onClick(View v) {
			finish();
		}
	};

	// 返回上级目录
	protected OnClickListener upDirBtnClickListener = new OnClickListener() {

		public void onClick(View v) {

			String strPath = currentFile.getAbsolutePath();
			if (strPath.equals("/") == true) {
				return;
			}

			currentFile = currentFile.getParentFile();

			TextView pathView = (TextView) findViewById(R.id.current_path);
			pathView.setText(currentFile.getAbsolutePath());

			if (currentFile.isDirectory())
				fill(currentFile);
		}
	};

	// 根目录
	protected OnClickListener rootDirBtnClickListener = new OnClickListener() {

		public void onClick(View v) {

			currentFile = new File("/");
			fill(currentFile);

			TextView pathView = (TextView) findViewById(R.id.current_path);
			pathView.setText(currentFile.getAbsolutePath());
		}
	};
}
